<template>
    <div>
        <div class="top">
            <div class="rightBtn">
                <span @click="toLogin">登录</span>
            </div>
        </div>
        <div class="register">
            <h2>注册页面</h2>
            <el-form :model="userInfo" :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm">

                <el-form-item label="用户名" prop="username">
                    <el-input v-model="userInfo.username" style="width:230px"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="userInfo.password" style="width:230px"></el-input>
                </el-form-item>
                <el-form-item label="头像" prop="avatar">
                    <el-upload class="avatar-uploader" action="http://124.222.242.111:8080/upload" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                        <img v-if="userInfo.avatar" :src="userInfo.avatar" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item label="性别" prop="gender">
                    <el-radio-group v-model="userInfo.gender">
                        <el-radio label="0">男</el-radio>
                        <el-radio label="1">女</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="生日" prop="birthday">
                    <el-date-picker v-model="userInfo.birthday" style="width:230px" type="date" placeholder="选择日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="电话" prop="phone">
                    <el-input v-model="userInfo.phone" style="width:230px"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button size="small" type="primary" @click="save('userInfo')">保存</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
import { postJSON } from "@/utils/request";

export default {
    data() {
        return {
            userInfo: {},
            rules: {
                username: [
                    {
                        required: true,
                        message: "用户名不能为空",
                        trigger: "blur",
                    },
                ],
                password: [
                    {
                        required: true,
                        message: "密码不能为空",
                        trigger: "blur",
                    },
                ],
            },
        };
    },

    created() {},
    methods: {
        // 头像上传成功之前
        beforeAvatarUpload(file) {
            const whiteList = [
                "image/webp",
                "image/jpeg",
                "image/jpg",
                "image/png",
            ];
            if (whiteList.indexOf(file.type) == -1) {
                this.$message.error("上传头像图片格式不正确!");
            }
            return whiteList;
        },
        // 头像上传成功后
        handleAvatarSuccess(res, file) {
            this.userInfo.avatar = res.data;
        },
        save() {
            postJSON("/user/register", {
                avatar: this.userInfo.avatar,
                birthday: this.userInfo.birthday,
                gender: this.userInfo.gender,
                password: this.userInfo.password,
                phone: this.userInfo.phone,
                username: this.userInfo.username,
            })
                .then((res) => {
                    console.log("注册", res);
                    if (res && res.message == "ok") {
                        this.$message({
                            message: "注册成功！",
                            type: "success",
                        });
                    }
                })
                .catch(() => {
                    this.$message.error("注册失败！");
                });
        },
        // 跳转到登录页面
        toLogin() {
            this.$router.push("/login");
        },
    },
};
</script>
<style lang="scss" scoped>
.top {
    height: 40px;
    background-color: #b11e22;

    .rightBtn {
        margin-right: 10%;
        float: right;
        line-height: 40px;
        color: #fff;
        span {
            margin-left: 20px;
            &:hover {
                cursor: pointer;
            }
        }
    }
}
.register {
    min-height: calc(100vh - 300px);
    width: 80%;
    margin: 0 auto;
    h2 {
        text-align: center;
        margin: 20px 0;
    }

    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409eff;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
}
</style>